package org.asdtiang.designer
import org.asdtiang.page.PageUtil;

class TechnologyTransferController {
	def authenticateService
	def sessionFactory
		static allowedMethods = [save: "POST", update: "POST", delete: "POST"]
	
		def index = {
			redirect(action: "list", params: params)
		}
		def list1={
			if(!params.total){
				params.total=TechnologyTransfer.count()
			}
			def hql="select id,title ,dateCreated from TechnologyTransfer "
			params.max = Math.min(params.max ? params.max as int : 2, 100)
			params.linkTotal = Math.min(params.linkTotal ? params.linkTotal as int : 2, 100)
			params.offset = Math.min(params.offset ? params.offset as int : 0, 100)
			params.sort=params.sort==null?"dateCreated":params.sort
			params.order=params.order==null?"desc":params.order
			params.selectMax="true"
			def results = sessionFactory.currentSession.createQuery(hql+" order by ${params.sort} ${params.order}")
			   .setCacheable(false)
			   .setReadOnly(true)
			   .setFirstResult(params.offset)
			   .setMaxResults(params.max)
			   .list()
			render view:"/technologyTransfer/list1",model:[pageAjaxList:results,paginateAjaxTotal:params.total]
		}
	
		def list = {
			params.max = Math.min(params.max ? params.max as int : 1, 100)
			params.offset = Math.min(params.offset ? params.offset as int : 0, 100)
			[technologyTransferInstanceList: TechnologyTransfer.list(params), technologyTransferInstanceTotal: TechnologyTransfer.count()]
		}
		def listByUserId={
			params.max = Math.min(params.max ? params.max as int : 1, 100)
			params.offset = Math.min(params.offset ? params.offset as int : 0, 100)
			def user=authenticateService.userDomain()
			if(null==user){
				redirect(controller:'login',action:'auth',params:[noLogin:"${message(code: 'noLogin')}"])
				return
			}
			def technologyTransferInstance = new TechnologyTransfer()
			def l=TechnologyTransfer.findAllByApply(user,[max:params.max, sort:"dateCreated", order:"desc", offset:params.offset])
			render view:"list",model:[technologyTransferInstanceList:l, technologyTransferInstanceTotal: TechnologyTransfer.findAllByApply(user).size()]
		}
		def create = {
			
			def b=PageUtil.getInstanceByName("TechnologyTransfer")
			log.info "sss"+ b.class
			def technologyTransferInstance = b
			def user=authenticateService.userDomain()
			log.info "user:"+user+";;;;"
			if(null==user){
				redirect(controller:'login',action:'auth',params:[noLogin:"${message(code: 'noLogin')}"])
				return
			}
			technologyTransferInstance.properties = params
			return [technologyTransferInstance: technologyTransferInstance,userId:user.id]
		}
	
		def save = {
			def technologyTransferInstance = new TechnologyTransfer(params)
			def baseUser=BaseUser.get(params.userId)
			technologyTransferInstance.apply=baseUser
			if (technologyTransferInstance.save(flush: true)) {
				flash.message = "${message(code: 'default.created.message', args: [message(code: 'technologyTransfer.label', default: 'TechnologyTransfer'), technologyTransferInstance.id])}"
				redirect(action: "show", id: technologyTransferInstance.id)
			}
			else {
				render(view: "create", model: [technologyTransferInstance: technologyTransferInstance])
			}
		}
	
		def show = {
			def technologyTransferInstance = TechnologyTransfer.get(params.id)
			if (!technologyTransferInstance) {
				flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'technologyTransfer.label', default: 'TechnologyTransfer'), params.id])}"
				redirect(action: "list")
			}
			else {
				[technologyTransferInstance: technologyTransferInstance]
			}
		}
	
		def edit = {
			def technologyTransferInstance = TechnologyTransfer.get(params.id)
			if (!technologyTransferInstance) {
				flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'technologyTransfer.label', default: 'TechnologyTransfer'), params.id])}"
				redirect(action: "list")
			}
			else {
				return [technologyTransferInstance: technologyTransferInstance]
			}
		}
	
		def update = {
			def technologyTransferInstance = TechnologyTransfer.get(params.id)
			if (technologyTransferInstance) {
				if (params.version) {
					def version = params.version.toLong()
					if (technologyTransferInstance.version > version) {
						
						technologyTransferInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'technologyTransfer.label', default: 'TechnologyTransfer')] as Object[], "Another user has updated this TechnologyTransfer while you were editing")
						render(view: "edit", model: [technologyTransferInstance: technologyTransferInstance])
						return
					}
				}
				technologyTransferInstance.properties = params
				if (!technologyTransferInstance.hasErrors() && technologyTransferInstance.save(flush: true)) {
					flash.message = "${message(code: 'default.updated.message', args: [message(code: 'technologyTransfer.label', default: 'TechnologyTransfer'), technologyTransferInstance.id])}"
					redirect(action: "show", id: technologyTransferInstance.id)
				}
				else {
					render(view: "edit", model: [technologyTransferInstance: technologyTransferInstance])
				}
			}
			else {
				flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'technologyTransfer.label', default: 'TechnologyTransfer'), params.id])}"
				redirect(action: "list")
			}
		}
	
		def delete = {
			def technologyTransferInstance = TechnologyTransfer.get(params.id)
			if (technologyTransferInstance) {
				try {
					technologyTransferInstance.delete(flush: true)
					flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'technologyTransfer.label', default: 'TechnologyTransfer'), params.id])}"
					redirect(action: "list")
				}
				catch (org.springframework.dao.DataIntegrityViolationException e) {
					flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'technologyTransfer.label', default: 'TechnologyTransfer'), params.id])}"
					redirect(action: "show", id: params.id)
				}
			}
			else {
				flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'technologyTransfer.label', default: 'TechnologyTransfer'), params.id])}"
				redirect(action: "list")
			}
		}
}
	
